package com.fbinggun.dt.gui;

import com.fbinggun.dt.DailyTask;
import com.fbinggun.dt.player.PlayerData;
import com.fbinggun.dt.task.Task;
import com.fbinggun.fbinggunutil.gui.SlotItem;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class TaskGui implements Listener {
    String title;
    int size;
    int taskSlot;
    public Map<String, SlotItem> otherSlot= new HashMap<>();
    public Map<String,Map<Integer,String>> temp = new HashMap<>();
    public TaskGui(ConfigurationSection configurationSection) {
        this.title = configurationSection.getString("Title");
        size=configurationSection.getInt("Size");
        taskSlot=configurationSection.getInt("Slot");
        loadGui(configurationSection.getConfigurationSection("Gui"));
        DailyTask.plugin.getServer().getPluginManager().registerEvents(this,DailyTask.plugin);
    }
    public void loadGui(ConfigurationSection configurationSection){
        if(configurationSection!=null){
            for (String taskId : configurationSection.getKeys(false)) {
                     ConfigurationSection section =configurationSection.getConfigurationSection(taskId);
                    otherSlot.put(taskId,new SlotItem(taskId,section));
            }
        }
    }


    public void openInventory(Player player) throws SQLException, ClassNotFoundException {
        PlayerData playerData = DailyTask.getPlayerData(player);
        if(playerData.getTask()!=null){
            Map<Integer,String> temp = new HashMap<>();
            Inventory inv = Bukkit.createInventory(null,size,title);
            Task task = playerData.getTask().getTask();
            ItemStack itemStack =task.getItem(player);
            inv.setItem(taskSlot,itemStack);
            temp.put(taskSlot,"taskGui");
            if(otherSlot!=null&&otherSlot.size()!=0){
                for(SlotItem item:otherSlot.values()){
                    if(item.getPermission()!=null){
                        if(!player.hasPermission(item.getPermission())){
                            continue;
                        }}
                    inv.setItem(item.getSlot(),item.getItem(player));
                    temp.put(item.getSlot(),item.getId());
                }
            }
            this.temp.put(player.getName(),temp);
            player.openInventory(inv);
        }
    }
    @EventHandler
    public void click(InventoryClickEvent event) {
        Player player = (Player) event.getWhoClicked();
        Map<Integer,String> slot = temp.get(player.getName());
        if(slot==null){
            return;
        }
        event.setCancelled(true);
    }
    @EventHandler
    public void click(InventoryCloseEvent event) {
        Player player = (Player) event.getPlayer();
        Map<Integer,String> slot = temp.get(player.getName());
        if(slot==null){
            return;
        }
        temp.remove(player.getName());
    }
}
